#include "Server_UDP.h"


static const int max_length = 1024;
char data_[max_length];

Server_UDP::Server_UDP(boost::asio::io_service& io_service, int port)
	: socket_(io_service, udp::endpoint(udp::v4(), port))
{
		start_receive();
}


Server_UDP::~Server_UDP(void)
{
}

void Server_UDP::start_receive(){
	socket_.async_receive_from(
	boost::asio::buffer(data_, max_length), remote_endpoint_,
	boost::bind(&Server_UDP::handle_receive, this,
	boost::asio::placeholders::error,
	boost::asio::placeholders::bytes_transferred));
}

void Server_UDP::handle_receive(const boost::system::error_code& error,
	std::size_t bytes_transferred)
{
	if (!error || error == boost::asio::error::message_size)
	{

		/*
		socket_.async_send_to(boost::asio::buffer(*message), remote_endpoint_,
			boost::bind(&udp_server::handle_send, this, message,
			boost::asio::placeholders::error,
			boost::asio::placeholders::bytes_transferred));
			*/

		/*size_t len = socket_.receive_from(
			boost::asio::buffer(recv_buffer_), remote_endpoint_);

		std::cout.write(recv_buffer_.data(), len);*/

		std::cout << "Received UI UDP: " << std::endl;
		std::cout.write(data_, bytes_transferred);

	}
	start_receive();
}
	
void Server_UDP::handle_send(boost::shared_ptr<std::string> message,
	const boost::system::error_code& error,
	std::size_t bytes_transferred)
{
}
